package com.base.lib.ui;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.base.lib.R;
import com.base.lib.mvp.present.BasePresenter;
import com.base.lib.mvp.view.IBaseView;
import com.base.lib.ui.dialog.LoadingDialog;
import com.base.lib.util.EventBusUtil;
import com.base.lib.util.ScreenUtils;
import com.base.lib.util.StringUtils;
import com.base.lib.util.ToastUtil;

/**
 * @author neo.duan
 * @date 2019-09-10 14:29
 * @desc Fragment基类
 */
public abstract class BaseAppFragment<P extends BasePresenter> extends Fragment implements IBaseView {

    public Context mContext;
    protected P mPresenter;
    public Toolbar mToolBar;
    TextView mTvLeft;
    TextView mTvRight;
    TextView mTvTitle;
    ViewGroup mContainer;
    LoadingDialog mLoadingDialog;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_base, null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initBaseView(view);

        Bundle bundle = getArguments();
        if (bundle != null) {
            initBundleData(bundle);
        }

        /***初始化标题栏****/
        initTop();
        /***初始化内容view****/
        initView();
        /***初始化事件监听****/
        initListeners();
        /***初始化present****/
        mPresenter = initPresenter();
        initData();
    }

    public void initBaseView(View view) {
        mToolBar = view.findViewById(R.id.base_toolBar);
        mTvLeft = view.findViewById(R.id.tv_left_text);
        mTvTitle = view.findViewById(R.id.tv_base_title);
        mTvRight = view.findViewById(R.id.tv_right_text);
        mContainer = view.findViewById(R.id.fl_content_container);
        int layoutResId = getLayoutResId();
        if (layoutResId > 0) {
            mContainer.removeAllViews();
            mContainer.addView(View.inflate(mContext, layoutResId, null));
        }

        if (useEventBus()) {
            EventBusUtil.register(this);
        }
    }

    public boolean useEventBus() {
        return false;
    }

    /**
     * 初始化Bundle传递的数据
     */
    public void initBundleData(Bundle bundle) {

    }

    protected void enableTop(boolean enable) {
        mToolBar.setVisibility(enable ? View.VISIBLE : View.GONE);
    }

    protected void enableBack(boolean enable) {
        mToolBar.setNavigationIcon(enable ?
                AppCompatResources.getDrawable(mContext, R.drawable.ic_arrow_back) : null);
        mTvLeft.setVisibility(enable ? View.VISIBLE : View.GONE);
    }

    protected void enableBack(boolean enable, View.OnClickListener listener) {
        enableBack(enable);
        mToolBar.setNavigationOnClickListener(listener);
    }

    protected void enableBack(int iconResId, View.OnClickListener listener) {
        mToolBar.setNavigationIcon(iconResId);
        mToolBar.setNavigationOnClickListener(listener);
    }

    protected void enableBack(String text, View.OnClickListener listener) {
        if (!TextUtils.isEmpty(text)) {
            mToolBar.setNavigationIcon(null);
            mTvLeft.setVisibility(View.VISIBLE);
            mTvLeft.setText(text);
            mTvLeft.setOnClickListener(listener);
        } else {
            mTvLeft.setVisibility(View.GONE);
        }
    }

    protected void setTitle(String title) {
        mTvTitle.setText(StringUtils.nullStrToEmpty(title));
    }

    protected void enableRight(boolean enable) {
        mTvRight.setVisibility(enable ? View.VISIBLE : View.GONE);
    }

    protected void enableRight(String text) {
        if (!TextUtils.isEmpty(text)) {
            mTvRight.setVisibility(View.VISIBLE);
            mTvRight.setText(text);
        } else {
            mTvRight.setVisibility(View.GONE);
        }
    }

    protected void enableRight(String text, View.OnClickListener listener) {
        if (!TextUtils.isEmpty(text)) {
            mTvRight.setVisibility(View.VISIBLE);
            mTvRight.setText(text);
            mTvRight.setOnClickListener(listener);
        } else {
            mTvRight.setVisibility(View.GONE);
        }
    }

    protected void enableRight(int menuResId, View.OnClickListener listener) {
        Drawable drawable = ContextCompat.getDrawable(mContext, menuResId);
        if (drawable != null) {
            mTvRight.setVisibility(View.VISIBLE);
            mTvRight.setText("");
            // 这一步必须要做,否则不会显示.
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            mTvRight.setCompoundDrawables(drawable, null, null, null);
            //设置padding,让图片居中显示
            mTvRight.setPadding(ScreenUtils.dpToPxInt(mContext, 16), 0, 0, 0);

            mTvRight.setOnClickListener(listener);
        }
    }

    @Override
    public void showLoading() {
        showLoading("正在加载");
    }

    @Override
    public void showLoading(String message) {
        if (!this.isSafe()) {
            return;
        }
        if (mLoadingDialog == null) {
            synchronized (LoadingDialog.class) {
                if (mLoadingDialog == null) {
                    mLoadingDialog = new LoadingDialog(mContext);
                }
            }
        }
        mLoadingDialog.setMessage(message);
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    @Override
    public void hideLoading() {
        if (null != mLoadingDialog) {
            if (mLoadingDialog.isShowing()) {
                mLoadingDialog.dismiss();
            }
        }
    }

    @Override
    public void showNetError() {

    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showMessage(String msg) {
        if (!isSafe() || TextUtils.isEmpty(msg)) {
            return;
        }
        ToastUtil.show(mContext.getApplicationContext(), msg);
    }

    @Nullable
    public final <T extends View> T findViewById(@IdRes int id) {
        return mContainer.findViewById(id);
    }

    @Override
    public void onDestroyView() {
        if (useEventBus()) {
            EventBusUtil.unregister(this);
        }
        super.onDestroyView();
    }

    /**
     * 判断当前fragment是否安全
     *
     * @return true or false
     */
    protected boolean isSafe() {
        return !(this.isRemoving() || this.getActivity() == null || this.isDetached() || !this.isAdded() || this.getView() == null);
    }

    protected abstract void initTop();

    protected abstract int getLayoutResId();

    protected abstract void initView();

    protected abstract void initListeners();

    protected abstract P initPresenter();

    protected abstract void initData();

}
